var timeRequiredToBuy = function (tickets, k) {
  let temp = tickets[k]
  let count = 0
  if (temp === 0) {
    return count
  } else {
    while (temp !== 0) {
      let data = tickets.shift() - 1
      tickets.push(data)
      count++
      temp--
    }
    return count
  }
};

console.log(timeRequiredToBuy([2, 3, 2], 2));